﻿using STT.Admin;
using STT.Business;
using STT.Entities;
using STT.Layer.Connection;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace STT.Profile.UserControlsGV
{
    public partial class UcDanhSachHocVien : System.Web.UI.UserControl
    {
        ToolBox tb = new ToolBox();
        string last;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                LoadDDLKhoahoc();
            
        }
        private void LoadDDLKhoahoc()
        {
            Methods.KhoaHoc2(ref ddlkhoahoc, Session["MaFgv"].ToString());
        }
        void ThongBao(string Loi)
        {
            ScriptManager.RegisterClientScriptBlock(this, GetType(), "Thông báo!","<script>alert('" + Loi + "')</script>", false);
        }
        protected void ddlkhoahoc_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (ddlkhoahoc.SelectedItem.ToString() == "---------Chọn---------")
                {
                    ThongBao("Bạn hãy chọn khóa học để xem..!");
                }
                else
                {
                    Methods.LopHocByKhoaHocID(ref ddllophoc, ddlkhoahoc.SelectedValue.ToString(), Session["MaFgv"].ToString());
                }
            }
            catch (Exception ex)
            {
                Response.Write("");
            }
        }

        protected void ddllophoc_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {

                if (ddllophoc.SelectedItem.ToString() == "---------Chọn---------")
                {
                    ThongBao("Bạn hãy chọn lớp học để xem..!");

                }
                else
                {
                    btnexport.NavigateUrl = "~/Report/DSHVProfileGV.aspx?ml=" + ddllophoc.SelectedValue.ToString();
                    LoadByLopHoc();
                }
            }
            catch (Exception ex)
            {
                Response.Write("");
            }
        }
        private void LoadByLopHoc()
        {
            
            ETbHocVien hv = new ETbHocVien();
            hv.MaLop = int.Parse(ddllophoc.SelectedValue.ToString());
            DataTable dt = BTbHocVien.SelectByLop(hv);
            if (dt.Rows.Count > 0)
            {
                gvDiemHV.DataSource = dt;
                gvDiemHV.DataBind(); 
                pnbangdiem.Visible = true;
            }
            else pnbangdiem.Visible = false;
        }
        protected void gvDiemHV_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            if (ddllophoc.SelectedItem.ToString() != "---------Chọn---------")
            {
                gvDiemHV.PageIndex = e.NewPageIndex;
                LoadByLopHoc();
            }
        }
        protected void ddlchucvutronglop_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                //ImageButton img = sender as ImageButton;
                DropDownList ddl = sender as DropDownList;
                foreach (GridViewRow row in gvDiemHV.Rows)
                {
                    ImageButton imgaccept = row.FindControl("imgaccept") as ImageButton;
                    Control ctrl = row.FindControl("ddlchucvutronglop") as DropDownList;
                    DropDownList ddlchucvu = ((DropDownList)row.FindControl("ddlchucvutronglop"));
                    if (ctrl != null)
                    {
                        DropDownList ddl1 = (DropDownList)ctrl;
                        if (ddl.ClientID == ddl1.ClientID)
                        {
                            if (ddlchucvu.SelectedValue.ToString() == "0")
                                imgaccept.Visible = false;
                            else
                            {
                                imgaccept.Visible = true;
                            }
                        }
                    }
                }
            }
            catch { }
        }
        protected void gvDiemHV_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    DropDownList ddlchucvu = (DropDownList)e.Row.FindControl("ddlchucvutronglop");
                    ImageButton imgdelete = (ImageButton)e.Row.FindControl("imgdelete");
                    LoadChucVuHVTrongLop(ddlchucvu);
                    if ((e.Row.FindControl("lblchucvutronglop") as Label).Text != "")
                    { 
                        ddlchucvu.Items.FindByText((e.Row.FindControl("lblchucvutronglop") as Label).Text).Selected = true;
                        ddlchucvu.Enabled = false;
                        imgdelete.Visible = true;
                    }
                    DataTable dt = tb.GetDataTableById2("TbHocVien_GetChucVuTrongLopByMaLop", ddllophoc.SelectedValue.ToString());
                    if (ddlchucvu.Enabled == true)
                    {
                        LoadChucVuHVTrongLop(ddlchucvu);
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            if (dt.Rows[i]["ChucVuTrongLop"].ToString() == "Lớp Trưởng")
                            {
                                ddlchucvu.Items.Remove(ddlchucvu.Items.FindByText("Lớp Trưởng"));
                            }
                            if (dt.Rows[i]["ChucVuTrongLop"].ToString() == "Lớp Phó")
                            {
                                ddlchucvu.Items.Remove(ddlchucvu.Items.FindByText("Lớp Phó"));
                            }
                            if (dt.Rows[i]["ChucVuTrongLop"].ToString() == "Bí Thư")
                            {
                                ddlchucvu.Items.Remove(ddlchucvu.Items.FindByText("Bí Thư"));
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Response.Write("");
            }
        }
        protected void imgaccept_Click(object sender, ImageClickEventArgs e)
        {
            try
            {

                ImageButton img = sender as ImageButton;
                foreach (GridViewRow row in gvDiemHV.Rows)
                {
                    string Mahv = gvDiemHV.DataKeys[row.RowIndex].Value.ToString();
                    ImageButton imgdelete = row.FindControl("imgdelete") as ImageButton;
                    ImageButton imgaccept = row.FindControl("imgaccept") as ImageButton;
                    DropDownList ddlchucvu2 = ((DropDownList)row.FindControl("ddlchucvutronglop"));
                    Control ctrl = row.FindControl("imgaccept") as ImageButton;
                    if (ctrl != null)
                    {
                        ImageButton img1 = (ImageButton)ctrl;
                        if (img.ClientID == img1.ClientID)
                        {
                            DataTable dt = tb.GetDataTableById4("TbHocVien_GetChucVuTrongLopByMaLopCV", ddlchucvu2.SelectedItem.ToString(), ddllophoc.SelectedValue.ToString());
                            if (ddlchucvu2.SelectedValue.ToString() != "0")
                            {
                                if (dt.Rows.Count > 0)
                                {
                                    ThongBao("Chức vụ này đã được cấp..!");
                                }
                                else
                                {
                                    imgdelete.Visible = true;
                                    imgaccept.Visible = false;
                                    ddlchucvu2.Enabled = false;
                                    BTbHocVien.UpdateChucVuHVTrongLop(new ETbHocVien
                                    {
                                        ChucVuTrongLop = ddlchucvu2.SelectedItem.ToString(),
                                        MaHV = Mahv
                                    });
                                    last = ddlchucvu2.SelectedItem.ToString();
                                }
                            }
                            else
                                ThongBao("Bạn hãy chọn chức vụ học viên..!");
                        }
                    }
                    foreach (GridViewRow row2 in gvDiemHV.Rows)
                    {
                        DropDownList ddlchucvu3 = ((DropDownList)row2.FindControl("ddlchucvutronglop"));
                        if (ddlchucvu3.Enabled == true)
                        {
                            ddlchucvu3.Items.Remove(ddlchucvu3.Items.FindByText(last));
                            imgaccept.Visible = false;
                        };
                    }
                }
            }
            catch { }
        }
        private void LoadChucVuHVTrongLop(DropDownList ddlchucvu)
        {
            ddlchucvu.Items.Clear();
            List<ListItem> items = new List<ListItem>();
            items.Add(new ListItem("Chọn", "0"));
            items.Add(new ListItem("Lớp Trưởng", "1"));
            items.Add(new ListItem("Lớp Phó", "2"));
            items.Add(new ListItem("Bí Thư", "3"));
            ddlchucvu.Items.AddRange(items.ToArray());
        }
        protected void imgdelete_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                ImageButton img = sender as ImageButton;
                foreach (GridViewRow row in gvDiemHV.Rows)
                {
                    string Mahv = gvDiemHV.DataKeys[row.RowIndex].Value.ToString();
                    ImageButton imgdelete = row.FindControl("imgdelete") as ImageButton;
                    ImageButton imgaccept = row.FindControl("imgaccept") as ImageButton;
                    DropDownList ddlchucvu = (DropDownList)row.FindControl("ddlchucvutronglop");
                    Control ctrl = row.FindControl("imgdelete") as ImageButton;
                    if (ctrl != null)
                    {
                        ImageButton img1 = (ImageButton)ctrl;
                        if (img.ClientID == img1.ClientID)
                        {
                            BTbHocVien.UpdateChucVuHVTrongLop(new ETbHocVien
                            {
                                ChucVuTrongLop = "",
                                MaHV = Mahv
                            });
                            imgdelete.Visible = false;
                            imgaccept.Visible = false;
                            ddlchucvu.Enabled = true;
                        }
                    }
                }
                foreach (GridViewRow row in gvDiemHV.Rows)
                {
                    DropDownList ddlchucvu = (DropDownList)row.FindControl("ddlchucvutronglop");
                    DataTable dt = tb.GetDataTableById2("TbHocVien_GetChucVuTrongLopByMaLop", ddllophoc.SelectedValue.ToString());
                    if (ddlchucvu.Enabled == true)
                    {
                        LoadChucVuHVTrongLop(ddlchucvu);
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            if (dt.Rows[i]["ChucVuTrongLop"].ToString() == "Lớp Trưởng")
                            {
                                ddlchucvu.Items.Remove(ddlchucvu.Items.FindByText("Lớp Trưởng"));
                            }
                            if (dt.Rows[i]["ChucVuTrongLop"].ToString() == "Lớp Phó")
                            {
                                ddlchucvu.Items.Remove(ddlchucvu.Items.FindByText("Lớp Phó"));
                            }
                            if (dt.Rows[i]["ChucVuTrongLop"].ToString() == "Bí Thư")
                            {
                                ddlchucvu.Items.Remove(ddlchucvu.Items.FindByText("Bí Thư"));
                            }
                        }
                    }
                }
            }
            catch 
            {
               
            }  
         
        }
    }
}